package com.lynx.interceptor;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.lynx.util.Const;

/**
 * @author chris.liu
 * @version 6/23/14 3:40 PM
 */
public class AccountInterceptor implements HandlerInterceptor {

	private List<String> excludedUrls;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception {
		// excluded URLs:
		// see http://stackoverflow.com/questions/9908124/spring-mvc-3-interceptor-on-all-excluding-some-defined-paths
		String requestUri = request.getRequestURI();

		// intercept
		HttpSession session = request.getSession();
		if (session.getAttribute(Const.SESSION_KEY_USER) == null) {
			String redirectPath = String.format("%s%s%s%s%s", request.getContextPath(),
					File.separator, Const.URL_MAP_NAV_ACCOUNT, File.separator,
					Const.URL_MAP_ACCOUNT_MY);
			response.sendRedirect(redirectPath);
			return false;
		} else {
			return true;
		}
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler, ModelAndView modelAndView) throws Exception {

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
			Object handler, Exception ex) throws Exception {

	}

	public void setExcludedUrls(List<String> excludedUrls) {
		this.excludedUrls = excludedUrls;
	}
}
